//
//  submitOrderViewController.m
//  lmq
//
//  Created by 千翼网络科技 on 2017/4/6.
//  Copyright © 2017年 yuanxin. All rights reserved.
//

#import "submitOrderViewController.h"
#import "configddd.h"
#import <Masonry/Masonry.h>
#import "PKYStepper.h"
#import "UIImageView+WebCache.h"
#import "payViewController.h"
#import "loginViewController.h"

@interface submitOrderViewController ()

@property(nonatomic,strong) UILabel *nameLable;
@property(nonatomic,strong) UIImageView *logoImg;
@property(nonatomic,strong) UILabel *shopNameLable;
@property(nonatomic,strong) UILabel *priceLable;
@property(nonatomic,strong) UIButton *subtractBtn;
@property(nonatomic,strong) UIButton *addBtn;
@property(nonatomic,strong) UILabel *numLable;
@property(nonatomic,strong) UILabel *allPriceLable;

@end

@implementation submitOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self requestData];
    [self initView];
    self.title = @"提交订单";
    self.view.backgroundColor = getColor(@"ffffff");
}

#pragma mark ------------------------------ init

- (void)initView{

    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(screenScale(20), screenScale(20), screenScale(40), screenScale(40))];
    img.image = [UIImage imageNamed:@"shop"];
    [self.view addSubview:img];
    
    _nameLable = [[UILabel alloc]init];
    _nameLable.text = @"店铺名称";
    _nameLable.font = get4sFont(12);
    _nameLable.textColor = getColor(@"565656");
    _nameLable.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:_nameLable];
    [_nameLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).with.offset(0);
        make.left.mas_equalTo(self.view).with.offset(screenScale(80));
        
        make.height.mas_equalTo(screenScale(72));
    }];
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, screenScale(72), scrWidth(), screenScale(140))];
    bgView.backgroundColor = getColor(@"ededed");
    [self.view addSubview:bgView];
    
    _logoImg = [[UIImageView alloc]init];
    [self.view addSubview:_logoImg];
    [_logoImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(bgView).with.offset(screenScale(20));
        make.left.mas_equalTo(bgView).with.offset(screenScale(20));
        make.width.mas_equalTo(screenScale(100));
        make.height.mas_equalTo(screenScale(100));
    }];
    
    _shopNameLable = [[UILabel alloc]init];
    _shopNameLable.text = @"商品名称";
    _shopNameLable.font = get4sFont(12);
    _shopNameLable.textColor = getColor(@"565656");
    _shopNameLable.textAlignment = NSTextAlignmentLeft;
    _shopNameLable.numberOfLines = 0;
    [self.view addSubview:_shopNameLable];
    [_shopNameLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(bgView).with.offset(screenScale(20));
        make.left.mas_equalTo(bgView).with.offset(screenScale(140));
        make.right.mas_equalTo(bgView).with.offset(screenScale(-20));
//        make.bottom.mas_equalTo(bgView).with.offset(screenScale(-20));
    }];
    
    _priceLable = [[UILabel alloc]init];
    _priceLable.text = @"0.01";
    _priceLable.font = get4sFont(11);
    _priceLable.numberOfLines = 0;
    _priceLable.textColor = getColor(@"fc0000");
    _priceLable.textAlignment = NSTextAlignmentRight;
    [self.view addSubview:_priceLable];
    [_priceLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(bgView).with.offset(screenScale(-20));
        make.right.mas_equalTo(bgView).with.offset(screenScale(-20));
        
        make.height.mas_equalTo(screenScale(24));
    }];
    
    UILabel *numLable = [[UILabel alloc]init];
    numLable.text = @"购买数量";
    numLable.textColor = getColor(@"737272");
    numLable.font = get4sFont(11);
    [self.view addSubview:numLable];
    [numLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(bgView).with.offset(screenScale(160));
        make.left.mas_equalTo(bgView).with.offset(screenScale(20));
        
        make.height.mas_equalTo(screenScale(40));
    }];
    
    _subtractBtn = [[UIButton alloc]init];
    _subtractBtn.layer.borderWidth = screenScale(1);
    _subtractBtn.layer.borderColor = getColor(@"737272").CGColor;
    _subtractBtn.titleLabel.font = get4sFont(11);
    _subtractBtn.titleLabel.textColor = getColor(@"737272");
    [_subtractBtn setTitle:@"-" forState:UIControlStateNormal];
    [_subtractBtn setTitleColor:getColor(@"737272") forState:UIControlStateNormal];
    [_subtractBtn addTarget:self action:@selector(subtractBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_subtractBtn];
    [_subtractBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(bgView).with.offset(screenScale(160));
        make.left.mas_equalTo(bgView).with.offset(screenScale(500));
        make.width.mas_equalTo(screenScale(40));
        make.height.mas_equalTo(screenScale(40));
    }];
    
    _numLable = [[UILabel alloc]init];
    _numLable.text = @"1";
    _numLable.font = get4sFont(11);
    _numLable.textColor = getColor(@"737272");
    _numLable.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_numLable];
    [_numLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_subtractBtn).with.offset(0);
        make.left.mas_equalTo(_subtractBtn.mas_right).with.offset(0);
        make.width.mas_equalTo(screenScale(40));
        make.height.mas_equalTo(screenScale(40));
    }];
    
    _addBtn = [[UIButton alloc]init];
    _addBtn.layer.borderWidth = screenScale(1);
    _addBtn.layer.borderColor = getColor(@"737272").CGColor;
    _addBtn.titleLabel.font = get4sFont(11);
    _addBtn.titleLabel.textColor = getColor(@"737272");
    [_addBtn setTitle:@"+" forState:UIControlStateNormal];
    [_addBtn setTitleColor:getColor(@"737272") forState:UIControlStateNormal];
    [_addBtn addTarget:self action:@selector(addBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_addBtn];
    [_addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(_subtractBtn).with.offset(0);
        make.left.mas_equalTo(_subtractBtn.mas_right).with.offset(screenScale(40));
        make.width.mas_equalTo(screenScale(40));
        make.height.mas_equalTo(screenScale(40));
    }];
    
    UIView *threadView = [[UIView alloc]init];
    threadView.backgroundColor = getColor(@"737272");
    [self.view addSubview:threadView];
    [threadView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_numLable).with.offset(0);
        make.left.mas_equalTo(_numLable).with.offset(0);
        make.width.mas_equalTo(screenScale(40));
        make.height.mas_equalTo(screenScale(1));
    }];
    
    UIView *threadView1 = [[UIView alloc]init];
    threadView1.backgroundColor = getColor(@"737272");
    [self.view addSubview:threadView1];
    [threadView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(_numLable).with.offset(0);
        make.left.mas_equalTo(_numLable).with.offset(0);
        make.width.mas_equalTo(screenScale(40));
        make.height.mas_equalTo(screenScale(1));
    }];
    
    //底部View
    UILabel *allLable = [[UILabel alloc]init];
    allLable.text = @"合计：";
    allLable.font = get4sFont(13);
    allLable.textColor = getColor(@"737272");
    allLable.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:allLable];
    [allLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view).with.offset(0);
        make.left.mas_equalTo(self.view).with.offset(screenScale(20));
        
        make.height.mas_equalTo(screenScale(70));
    }];
    
    UIButton *submitBtn=[[UIButton alloc]init];
    submitBtn.titleLabel.font = get4sFont(14);
    [submitBtn setTitle:@"提交订单" forState:UIControlStateNormal];
    [submitBtn addTarget:self action:@selector(submitBtnClick) forControlEvents:UIControlEventTouchUpInside];
    submitBtn.backgroundColor=[ZJCModel colorWithString:@"222222"];
    [self.view addSubview:submitBtn];
    [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view).with.offset(0);
        make.right.mas_equalTo(self.view).with.offset(0);
        make.width.mas_equalTo(screenScale(280));
        make.height.mas_equalTo(screenScale(70));
    }];
    
    _allPriceLable = [[UILabel alloc]init];
    _allPriceLable.text = @"¥100.00";
    _allPriceLable.font = get4sFont(13);
    _allPriceLable.textColor = getColor(@"fc0000");
    _allPriceLable.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:_allPriceLable];
    [_allPriceLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view).with.offset(0);
        make.right.mas_equalTo(submitBtn.mas_left).with.offset(screenScale(-20));
        
        make.height.mas_equalTo(screenScale(70));
    }];
    
    UIView *threadView2 = [[UIView alloc]init];
    threadView2.backgroundColor = [UIColor grayColor];
    [self.view addSubview:threadView2];
    [threadView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view).with.offset(screenScale(-70));
        make.left.mas_equalTo(self.view).with.offset(0);
        make.width.mas_equalTo(scrWidth());
        make.height.mas_equalTo(screenScale(1));
    }];
    
}

#pragma mark ------------------------------ click

- (void)subtractBtnClick{

    if ([_numLable.text intValue] > 1) {
        
        _numLable.text = [NSString stringWithFormat:@"%d",[_numLable.text intValue] - 1];
    }
    _allPriceLable.text = [@"¥" stringByAppendingString: [NSString stringWithFormat:@"%.2f",[_numLable.text floatValue] * [dataDic[@"price"] floatValue]]];
}

- (void)addBtnClick{
    
    if ([_numLable.text intValue] < 99) {
        
        _numLable.text = [NSString stringWithFormat:@"%d",[_numLable.text intValue] + 1];
    }
    _allPriceLable.text = [@"¥" stringByAppendingString: [NSString stringWithFormat:@"%.2f",[_numLable.text floatValue] * [dataDic[@"price"] floatValue]]];
}

- (void)submitBtnClick{
    
    // 登陆判断
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    [productParams setValue: [PublicFunction getConfigWithKey:@"token"] forKey:@"token"];
    
    [[MNetworkHandler sharedInstance]conURL:Is_Login
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       
                                       [self submitOrder];
                                       [hud removeFromSuperview];
                                   }else{
                                       
                                       [hud removeFromSuperview];
                                       loginViewController *loginVc =[[loginViewController alloc]init];
                                       [self.navigationController presentViewController:loginVc animated:YES completion:^{}];
                                   }
                               } failureBlock:^(NSError *error) {
                                   
                                   [hud removeFromSuperview];
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
}

- (void)submitOrder{

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    [productParams setValue: _shop_id forKey:@"goods_id"];
    [productParams setValue:[PublicFunction getConfigWithKey:@"token"] forKey:@"token"];
    [productParams setValue:_numLable.text forKey:@"num"];
    
    [[MNetworkHandler sharedInstance]conURL:Add_Order
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       
                                       [hud removeFromSuperview];
                                       
                                       [PublicFunction saveConfig:[returnData[@"data"] objectForKey:@"number"] forKey:@"number"];
                                       
                                       NSLog(@"returnData%@",returnData);
                                       
                                       payViewController *payVC = [[payViewController alloc]init];
                                       payVC.str = [NSString stringWithFormat:@"%.2f",[_numLable.text floatValue] * [dataDic[@"price"] floatValue]];
                                       payVC.number = [returnData[@"data"] objectForKey:@"number"];
                                       [self.navigationController pushViewController:payVC animated:YES];
                                   }
                                   else if ([[returnData objectForKey:@"code"]intValue]==-1){
                                       
                                       [hud removeFromSuperview];
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                       
                                   }
                                   else{
                                       
                                       [hud removeFromSuperview];
                                   }
                               } failureBlock:^(NSError *error) {
                                   
                                   [hud removeFromSuperview];
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
}

#pragma mark ------------------------------ requestData

- (void)requestData{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    [productParams setValue: _shop_id forKey:@"id"];
    [productParams setValue:[PublicFunction getConfigWithKey:@"token"] forKey:@"token"];
    
    [[MNetworkHandler sharedInstance]conURL:Confirm_Order
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       
                                       dataDic = [returnData objectForKey:@"data"];
                                       NSLog(@"==\n\n\n==dataDic====%@",dataDic);
                                       
                                       _nameLable.text = dataDic[@"name"];
                                       _shopNameLable.text = dataDic[@"shop_name"];
                                       _priceLable.text = [@"¥" stringByAppendingString: dataDic[@"price"]];
                                       _allPriceLable.text = [@"¥" stringByAppendingString: dataDic[@"price"]];
                                       NSString *urlWare = dataDic[@"logo"];
                                       NSString *urlN = manage(AP_HOST, urlWare);
                                       NSString *strUrl = [urlN stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
                                       NSURL *urlW = [NSURL URLWithString:strUrl];
                                       [_logoImg sd_setImageWithURL:urlW placeholderImage:[UIImage imageNamed:@"niunai"]];
                                       
                                       [hud removeFromSuperview];
                                   }
                                   else if ([[returnData objectForKey:@"code"]intValue]==-1){
                                       
                                       [hud removeFromSuperview];
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                       
                                   }
                                   else{
                                       
                                       [hud removeFromSuperview];
                                   }
                               } failureBlock:^(NSError *error) {
                                   
                                   [hud removeFromSuperview];
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
}

@end
